var result = {
  timestamp: "1627537908918",
  success: 0,
  rows: [
    {
      id: 3103,
      examId: 70,
      unitName: "各市（地）",
      pid: null,
      examUnits: [
        {
          id: 3108,
          examId: 70,
          unitName: "拉萨市",
          pid: 3103,
          examUnits: [
            {
              id: 3115,
              examId: 70,
              unitName: "城关区",
              pid: 3108,
              examUnits: null,
            },
            {
              id: 3116,
              examId: 70,
              unitName: "林周县",
              pid: 3108,
              examUnits: null,
            },
            {
              id: 3117,
              examId: 70,
              unitName: "当雄县",
              pid: 3108,
              examUnits: null,
            },
            {
              id: 3118,
              examId: 70,
              unitName: "尼木县",
              pid: 3108,
              examUnits: null,
            },
            {
              id: 3119,
              examId: 70,
              unitName: "曲水县",
              pid: 3108,
              examUnits: null,
            },
            {
              id: 3120,
              examId: 70,
              unitName: "堆龙德庆区",
              pid: 3108,
              examUnits: null,
            },
            {
              id: 3121,
              examId: 70,
              unitName: "达孜区",
              pid: 3108,
              examUnits: null,
            },
            {
              id: 3122,
              examId: 70,
              unitName: "墨竹工卡县",
              pid: 3108,
              examUnits: null,
            },
            {
              id: 3189,
              examId: 70,
              unitName: "其他",
              pid: 3108,
              examUnits: null,
            },
          ],
        },
        {
          id: 3109,
          examId: 70,
          unitName: "日喀则市",
          pid: 3103,
          examUnits: [
            {
              id: 3123,
              examId: 70,
              unitName: "桑珠孜区",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3124,
              examId: 70,
              unitName: "南木林县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3125,
              examId: 70,
              unitName: "江孜县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3126,
              examId: 70,
              unitName: "定日县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3127,
              examId: 70,
              unitName: "萨迦县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3128,
              examId: 70,
              unitName: "拉孜县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3129,
              examId: 70,
              unitName: "昂仁县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3130,
              examId: 70,
              unitName: "谢通门县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3131,
              examId: 70,
              unitName: "白朗县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3132,
              examId: 70,
              unitName: "仁布县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3133,
              examId: 70,
              unitName: "康马县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3134,
              examId: 70,
              unitName: "定结县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3135,
              examId: 70,
              unitName: "仲巴县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3136,
              examId: 70,
              unitName: "亚东县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3137,
              examId: 70,
              unitName: "吉隆县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3138,
              examId: 70,
              unitName: "聂拉木县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3139,
              examId: 70,
              unitName: "萨嘎县",
              pid: 3109,
              examUnits: null,
            },
            {
              id: 3140,
              examId: 70,
              unitName: "岗巴县",
              pid: 3109,
              examUnits: null,
            },
          ],
        },
        {
          id: 3110,
          examId: 70,
          unitName: "昌都市",
          pid: 3103,
          examUnits: [
            {
              id: 3141,
              examId: 70,
              unitName: "卡若区",
              pid: 3110,
              examUnits: null,
            },
            {
              id: 3142,
              examId: 70,
              unitName: "江达县",
              pid: 3110,
              examUnits: null,
            },
            {
              id: 3143,
              examId: 70,
              unitName: "贡觉县",
              pid: 3110,
              examUnits: null,
            },
            {
              id: 3144,
              examId: 70,
              unitName: "类乌齐县",
              pid: 3110,
              examUnits: null,
            },
            {
              id: 3145,
              examId: 70,
              unitName: "丁青县",
              pid: 3110,
              examUnits: null,
            },
            {
              id: 3146,
              examId: 70,
              unitName: "察雅县",
              pid: 3110,
              examUnits: null,
            },
            {
              id: 3147,
              examId: 70,
              unitName: "八宿县",
              pid: 3110,
              examUnits: null,
            },
            {
              id: 3148,
              examId: 70,
              unitName: "左贡县",
              pid: 3110,
              examUnits: null,
            },
            {
              id: 3149,
              examId: 70,
              unitName: "芒康县",
              pid: 3110,
              examUnits: null,
            },
            {
              id: 3150,
              examId: 70,
              unitName: "洛隆县",
              pid: 3110,
              examUnits: null,
            },
            {
              id: 3151,
              examId: 70,
              unitName: "边坝县",
              pid: 3110,
              examUnits: null,
            },
          ],
        },
        {
          id: 3111,
          examId: 70,
          unitName: "山南市",
          pid: 3103,
          examUnits: [
            {
              id: 3152,
              examId: 70,
              unitName: "乃东区",
              pid: 3111,
              examUnits: null,
            },
            {
              id: 3153,
              examId: 70,
              unitName: "扎囊县",
              pid: 3111,
              examUnits: null,
            },
            {
              id: 3154,
              examId: 70,
              unitName: "贡嘎县",
              pid: 3111,
              examUnits: null,
            },
            {
              id: 3155,
              examId: 70,
              unitName: "桑日县",
              pid: 3111,
              examUnits: null,
            },
            {
              id: 3156,
              examId: 70,
              unitName: "琼结县",
              pid: 3111,
              examUnits: null,
            },
            {
              id: 3157,
              examId: 70,
              unitName: "曲松县",
              pid: 3111,
              examUnits: null,
            },
            {
              id: 3158,
              examId: 70,
              unitName: "措美县",
              pid: 3111,
              examUnits: null,
            },
            {
              id: 3159,
              examId: 70,
              unitName: "洛扎县",
              pid: 3111,
              examUnits: null,
            },
            {
              id: 3160,
              examId: 70,
              unitName: "加查县",
              pid: 3111,
              examUnits: null,
            },
            {
              id: 3161,
              examId: 70,
              unitName: "隆子县",
              pid: 3111,
              examUnits: null,
            },
            {
              id: 3162,
              examId: 70,
              unitName: "错那县",
              pid: 3111,
              examUnits: null,
            },
            {
              id: 3163,
              examId: 70,
              unitName: "浪卡子县",
              pid: 3111,
              examUnits: null,
            },
          ],
        },
        {
          id: 3112,
          examId: 70,
          unitName: "那曲市",
          pid: 3103,
          examUnits: [
            {
              id: 3164,
              examId: 70,
              unitName: "色尼区",
              pid: 3112,
              examUnits: null,
            },
            {
              id: 3165,
              examId: 70,
              unitName: "嘉黎县",
              pid: 3112,
              examUnits: null,
            },
            {
              id: 3166,
              examId: 70,
              unitName: "比如县",
              pid: 3112,
              examUnits: null,
            },
            {
              id: 3167,
              examId: 70,
              unitName: "聂荣县",
              pid: 3112,
              examUnits: null,
            },
            {
              id: 3168,
              examId: 70,
              unitName: "安多县",
              pid: 3112,
              examUnits: null,
            },
            {
              id: 3169,
              examId: 70,
              unitName: "申扎县",
              pid: 3112,
              examUnits: null,
            },
            {
              id: 3170,
              examId: 70,
              unitName: "索县",
              pid: 3112,
              examUnits: null,
            },
            {
              id: 3171,
              examId: 70,
              unitName: "班戈县",
              pid: 3112,
              examUnits: null,
            },
            {
              id: 3172,
              examId: 70,
              unitName: "巴青县",
              pid: 3112,
              examUnits: null,
            },
            {
              id: 3173,
              examId: 70,
              unitName: "尼玛县",
              pid: 3112,
              examUnits: null,
            },
            {
              id: 3174,
              examId: 70,
              unitName: "双湖县",
              pid: 3112,
              examUnits: null,
            },
          ],
        },
        {
          id: 3113,
          examId: 70,
          unitName: "阿里地区",
          pid: 3103,
          examUnits: [
            {
              id: 3175,
              examId: 70,
              unitName: "普兰县",
              pid: 3113,
              examUnits: null,
            },
            {
              id: 3176,
              examId: 70,
              unitName: "札达县",
              pid: 3113,
              examUnits: null,
            },
            {
              id: 3177,
              examId: 70,
              unitName: "噶尔县",
              pid: 3113,
              examUnits: null,
            },
            {
              id: 3178,
              examId: 70,
              unitName: "日土县",
              pid: 3113,
              examUnits: null,
            },
            {
              id: 3179,
              examId: 70,
              unitName: "革吉县",
              pid: 3113,
              examUnits: null,
            },
            {
              id: 3180,
              examId: 70,
              unitName: "改则县",
              pid: 3113,
              examUnits: null,
            },
            {
              id: 3181,
              examId: 70,
              unitName: "措勤县",
              pid: 3113,
              examUnits: null,
            },
          ],
        },
        {
          id: 3114,
          examId: 70,
          unitName: "林芝市",
          pid: 3103,
          examUnits: [
            {
              id: 3182,
              examId: 70,
              unitName: "巴宜区",
              pid: 3114,
              examUnits: null,
            },
            {
              id: 3183,
              examId: 70,
              unitName: "工布江达",
              pid: 3114,
              examUnits: null,
            },
            {
              id: 3184,
              examId: 70,
              unitName: "米林县",
              pid: 3114,
              examUnits: null,
            },
            {
              id: 3185,
              examId: 70,
              unitName: "墨脱县",
              pid: 3114,
              examUnits: null,
            },
            {
              id: 3186,
              examId: 70,
              unitName: "波密县",
              pid: 3114,
              examUnits: null,
            },
            {
              id: 3187,
              examId: 70,
              unitName: "察隅县",
              pid: 3114,
              examUnits: null,
            },
            {
              id: 3188,
              examId: 70,
              unitName: "朗县",
              pid: 3114,
              examUnits: null,
            },
          ],
        },
      ],
    },
    {
      id: 3104,
      examId: 70,
      unitName: "区（中）直各单位",
      pid: null,
      examUnits: [
        {
          id: 3105,
          examId: 70,
          unitName: "党政机关",
          pid: 3104,
          examUnits: [],
        },
        {
          id: 3106,
          examId: 70,
          unitName: "事业单位",
          pid: 3104,
          examUnits: [],
        },
        {
          id: 3107,
          examId: 70,
          unitName: "国有企业",
          pid: 3104,
          examUnits: [],
        },
      ],
    },
  ],
  data: null,
  total: 0,
  totalPage: 0,
  totalSize: 0,
  page: 1,
  pageSize: 1,
  msg: null,
  ret: 0,
  stateCode: 0,
};

var provinces = [],
  citys = {},
  areas = {},
  depth = 1;

function handler(data, depth, parent_id, parent_name) {
  if (Array.isArray(data)) {
    data.forEach(({ id, unitName, examUnits }) => {
      switch (true) {
        case depth === 1:
          provinces.push({ id, name: unitName });
          break;
        case depth === 2:
          if (!citys[parent_id]) citys[parent_id] = [];
          citys[parent_id].push({ id, name: unitName, province: parent_name });
          break;
        case depth === 3:
          if (!areas[parent_id]) areas[parent_id] = [];
          areas[parent_id].push({ id, name: unitName, province: parent_name });
          break;
        default:
          break;
      }
      if (Array.isArray(examUnits)) handler(examUnits, depth + 1, id, unitName);
    });
  }
}

handler(result.rows, depth);

